from backend.task_manager import cancel_task, get_all_tasks, get_task_result, rerun_task
import pandas as pd
import streamlit as st


if __name__ == "__main__":

    st.title("任务中心")

    tasks = get_all_tasks()
    choose = st.dataframe(
        pd.DataFrame(tasks), on_select="rerun", selection_mode="single-row"
    )
    if choose.selection.rows:
        info = tasks[choose.selection.rows[0]]
        st.subheader("选择的任务详情")
        st.json(info)

        if info["status"] in ("running", "wait") and st.button("终止任务"):
            cancel_task(info["task_id"])
        elif info["status"] in ("cancelled",) and st.button("重跑任务"):
            rerun_task(info["task_id"])

        elif info["status"] in ("finished",) and st.button("展开结果"):
            result = get_task_result(info["task_id"])
            print("运行结果是", info["task_id"], result)
            st.json(result)
